﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.JScript.Compiler.ParseTree;
using Microsoft.JScript.Compiler;

namespace Yuyi.PluginForJS.Addin
{
    public class FunctionNode : AbstractNode
    {
        private string Name;

        private string ParamterString;

        private int startLine;

        private int endLine;

        private int startColumn;

        private int endColumn;

        private IList<AbstractNode> _childNodes;

        private IFunctionStrategy strategy;

        public FunctionNode(IFunctionStrategy strategy,string name)
        {
            this._childNodes = new List<AbstractNode>();
            this.strategy = strategy;

            FunctionDefinition function = strategy.GetFunction();

            startLine = function.Location.StartLine;
            endLine = function.Location.EndLine;
            startColumn = function.Body.Location.StartColumn + 1;
            endColumn = function.Location.EndColumn;


            if (!string.IsNullOrEmpty(name))
            {
                this.Name = name;
            }
            else if (function.Name != null)
            {
                this.Name = function.Name.Spelling;
            }
            else
            {
                this.Name = "Anonymous";
            }
            
            StringBuilder parameterString = new StringBuilder();

            IList<Parameter> parameters = function.Parameters;
            if (parameters != null && parameters.Count > 0)
            {
                foreach (Parameter p in parameters)
                {
                    parameterString.AppendFormat("{0},", p.Name.Spelling);
                }
                parameterString = parameterString.Remove(parameterString.Length - 1, 1);
            }
            this.ParamterString = parameterString.ToString();
            this._childNodes = Helper.CreateNodes(function.Body.Children);
        }

        #region INode Members

        public override string ImageKey
        {
            get { return "function"; }
        }
        public override IList<AbstractNode> ChildNodes
        {
            get { return _childNodes; }
        }
        #endregion

        public override string ToString()
        {
            return string.Format("{0}({1})",this.Name,this.ParamterString);
        }
        public override int StartLine
        {
            get {
                return startLine; }
        }

        public override int EndLine
        {
            get { return endLine+1; }
        }

        public override int StartColumn
        {
            get 
            {
                if (startColumn <= 0)
                    return 1;
                return startColumn; 
            }
        }

        public override int EndColumn
        {
            get 
            {
                if (endColumn <= 0)
                    return 1;
                return endColumn; 
            }
        }
    }
}
